package com.haoshui.utils;

import com.haoshui.domain.entity.LoginUser;
import com.haoshui.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("SecurityUtils")
public class SecurityUtils
{

    @Autowired
    private  UserRoleService userRoleService;

    /**
     * 获取用户
     **/
    public static LoginUser getLoginUser()
    {
        return (LoginUser) getAuthentication().getPrincipal();
    }

    /**
     * 获取Authentication
     */
    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    public  Boolean isAdmin(){
        Long id = getLoginUser().getUser().getId();
        //获取角色role
        List<Long> roleIdsByUserId = userRoleService.getRoleIdsByUserId(id);
        if (userRoleService==null){
            System.out.println("null");
        }
        if(roleIdsByUserId.contains(1L)){
            return true;
        }
        return id != null && 1L == id;
    }

    public static Long getUserId() {
        return getLoginUser().getUser().getId();
    }


}